package projet.volleystat;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class PrepMatchActivity extends Activity {
	protected Button selectmyteamb;
	protected Button selectadvteamb;
	protected Button placeplayersb;
	protected int myteamid;
	protected int advteamid;
	protected TreeMap<Integer, Team> selectableteams;
	protected VolleyRepository volleyDataBase;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_prep_match);

		selectmyteamb = (Button) findViewById(R.id.selectmyteamButton);
		selectadvteamb = (Button) findViewById(R.id.selectadvteamButton);
		placeplayersb = (Button) findViewById(R.id.placeplayersButton);

		myteamid = 0;
		advteamid = 0;

		volleyDataBase = new VolleyRepository(getApplicationContext());
		volleyDataBase.open();

		ArrayList<Team> selectableteamsarray = new ArrayList<Team>();
		selectableteams = new TreeMap<Integer, Team>();

		try {
			selectableteamsarray = volleyDataBase.getAllTeam();
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < selectableteamsarray.size(); i++) {
			selectableteams.put(selectableteamsarray.get(i).getId(),
					selectableteamsarray.get(i));
		}

		selectmyteamb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<Integer> teamsidarray = new ArrayList<Integer>();

				for (Map.Entry<Integer, Team> entry : selectableteams
						.entrySet()) {
					teamsidarray.add(entry.getKey());
				}

				Intent intent = new Intent(getApplicationContext(),
						SelectTeamActivity.class);
				intent.putExtra("teamsidarray", teamsidarray);
				intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 0);
			}
		});

		selectadvteamb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<Integer> teamsidarray = new ArrayList<Integer>();

				for (Map.Entry<Integer, Team> entry : selectableteams
						.entrySet()) {
					teamsidarray.add(entry.getKey());
				}

				Intent intent = new Intent(getApplicationContext(),
						SelectTeamActivity.class);
				intent.putExtra("teamsidarray", teamsidarray);
				intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, 1);
			}
		});

		placeplayersb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out.println(myteamid);
				System.out.println(advteamid);
				Intent intent = new Intent(getApplicationContext(),
						PlacePlayersActivity.class);
				intent.putExtra("myteamid", myteamid);
				intent.putExtra("advteamid", advteamid);
				intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0:
			if (resultCode == Activity.RESULT_OK) {
				if (myteamid != 0) {
					volleyDataBase.open();
					try {
						selectableteams.put(myteamid,
								volleyDataBase.getTeamById(myteamid));
					} catch (DatabaseException e) {
						e.printStackTrace();
					}
					volleyDataBase.close();
				}
				myteamid = data.getIntExtra("selectedteamid", -1);
				selectableteams.remove(myteamid);
				break;
			}
		case 1:
			if (resultCode == Activity.RESULT_OK) {
				if (advteamid != 0) {
					volleyDataBase.open();
					try {
						selectableteams.put(advteamid,
								volleyDataBase.getTeamById(advteamid));
					} catch (DatabaseException e) {
						e.printStackTrace();
					}
					volleyDataBase.close();
				}
				advteamid = data.getIntExtra("selectedteamid", -1);
				selectableteams.remove(advteamid);
				break;
			}
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		volleyDataBase.close();
		overridePendingTransition(0, 0);
	}
}
